package com.crazymaker.demo.hystrix;

import com.netflix.hystrix.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

@Slf4j
public class HystryxCommandExcecuteDemo {

    private static final int COUNT = 5;

    @Test
    public void testHttpGetterCommand() throws Exception {
        HttpGetterCommand command = new HttpGetterCommand("", SetterDemo.buildSetter(
                "group-1", "command-1", "threadpool-1"
        ));
        String result = command.execute();
        log.info("result = {}", result);
    }

    @Test
    public void testExecute() throws Exception {
        HystrixCommand.Setter setter = SetterDemo.buildSetter(
                "group-1","testCommandKey", "testThreadPool"
        );

        for (int i = 0; i < COUNT; i++) {
            new HttpGetterCommand("", setter).execute();
        }

        Thread.sleep(0x7fffffff);
    }

    @Test
    public void testQueue() throws Exception {
        HystrixCommand.Setter setter = SetterDemo.buildSetter(
                "group-1","testCommandKey", "testThreadPool"
        );
        List<Future<String>> list = new ArrayList<>();
        for (int i = 0; i < COUNT; i++) {
            Future<String> future = new HttpGetterCommand("", setter).queue();
            list.add(future);
        }

        int count = 1;
        for (Future<String> future : list) {
            String resu = future.get();
            log.info("第{}次请求得结果:{}", count++, resu);
        }
        Thread.sleep(0x7fffffff);
    }

}
